cmake_minimum_required(VERSION 2.8.3)
project(map_organizer)

## Find catkin and any catkin packages
find_package(catkin REQUIRED
  COMPONENTS
    cmake_modules
    roscpp

    eigen_conversions
    geometry_msgs
    map_server
    nav_msgs
    pcl_ros
    sensor_msgs
    tf
    tf_conversions

    map_organizer_msgs
    neonavigation_common
)
find_package(PCL REQUIRED)
find_package(Eigen3 REQUIRED)

find_package(PkgConfig)
pkg_check_modules(NEW_YAMLCPP yaml-cpp>=0.5)
if(NEW_YAMLCPP_FOUND)
  add_definitions(-DHAVE_NEW_YAMLCPP)
endif()

## Declare a catkin package
catkin_package(
  CATKIN_DEPENDS
    roscpp

    eigen_conversions
    geometry_msgs
    map_server
    nav_msgs
    pcl_ros
    sensor_msgs
    tf
    tf_conversions

    map_organizer_msgs
    neonavigation_common
)
include_directories(${catkin_INCLUDE_DIRS} ${PCL_INCLUDE_DIRS} ${Eigen3_INCLUDE_DIRS})
add_definitions(${PCL_DEFINITIONS})

# Workaround for debian stretch build (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894656)
list(REMOVE_ITEM PCL_LIBRARIES
  "vtkGUISupportQt"
  "vtkGUISupportQtOpenGL"
  "vtkGUISupportQtSQL"
  "vtkGUISupportQtWebkit"
  "vtkViewsQt"
  "vtkRenderingQt"
)

# Workaround for the bug in PCL(<1.8.1) https://github.com/PointCloudLibrary/pcl/issues/1406
remove_definitions(-DDISABLE_LIBUSB-1.0)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
  message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations -DROS_DISTRO_$ENV{ROS_DISTRO}=1")

# Binary installed pcl provided by Linux distro is built with -march=native
# which causes a lot of compatibility problems.
# Define PCL_NO_PRECOMPILE to disable using the binary version.
add_definitions(-DPCL_NO_PRECOMPILE)


add_executable(pointcloud_to_maps src/pointcloud_to_maps.cpp)
target_link_libraries(pointcloud_to_maps ${catkin_LIBRARIES} ${PCL_LIBRARIES})
add_dependencies(pointcloud_to_maps ${catkin_EXPORTED_TARGETS})

add_executable(tie_maps src/tie_maps.cpp)
target_link_libraries(tie_maps yaml-cpp ${catkin_LIBRARIES} ${PCL_LIBRARIES})
add_dependencies(tie_maps ${catkin_EXPORTED_TARGETS})

add_executable(save_maps src/save_maps.cpp)
target_link_libraries(save_maps ${catkin_LIBRARIES} ${PCL_LIBRARIES})
add_dependencies(save_maps ${catkin_EXPORTED_TARGETS})

add_executable(select_map src/select_map.cpp)
target_link_libraries(select_map ${catkin_LIBRARIES} ${PCL_LIBRARIES})
add_dependencies(select_map ${catkin_EXPORTED_TARGETS})

add_executable(pose_transform src/pose_transform.cpp)
target_link_libraries(pose_transform ${catkin_LIBRARIES} ${PCL_LIBRARIES})
add_dependencies(pose_transform ${catkin_EXPORTED_TARGETS})


if(CATKIN_ENABLE_TESTING)
  find_package(rostest REQUIRED)
  find_package(roslint REQUIRED)
  roslint_cpp()
  roslint_add_test()
endif()


install(TARGETS
    pointcloud_to_maps
    pose_transform
    save_maps
    select_map
    tie_maps
  ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
  RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
